package com.yunshipei.ui.fragment;


import android.net.http.SslError;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.yunshipei.common.wedigt.YspTitleBar;
import com.yunshipei.enterplorer.R;
import com.yunshipei.model.YspEvent;

import butterknife.Bind;
import io.rong.eventbus.EventBus;

public class AboutDetailFragment extends BaseBodyFragment {

    private static final String ARGS_URL = "args.url";
    private static final String ARGS_NAME = "args.name";

    private String url;
    private String name;

    @Bind(R.id.title_bar)
    protected YspTitleBar titleBar;
    @Bind(R.id.wv_about_detail)
    protected WebView webView;
    @Bind(R.id.net_state)
    protected TextView tvNetState;

    public static AboutDetailFragment newInstance(String url, String name) {
        AboutDetailFragment aboutDetailFragment = new AboutDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ARGS_URL, url);
        bundle.putString(ARGS_NAME, name);
        aboutDetailFragment.setArguments(bundle);
        return aboutDetailFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        url = getArguments().getString(ARGS_URL);
        name = getArguments().getString(ARGS_NAME);
    }

    @Override
    public int viewLayout() {
        return R.layout.fragment_about_detail;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        titleBar.setTitle(name);
        if (isPad()) {
            titleBar.setBackClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EventBus.getDefault().post(new YspEvent.PadClick(AboutFragment.class.getName()));
                }
            });
        }
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                webView.setVisibility(View.GONE);
                tvNetState.setVisibility(View.VISIBLE);
            }
        });
        webView.setWebChromeClient(new WebChromeClient());
        webView.loadUrl(url);
        tvNetState.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvNetState.setVisibility(View.GONE);
                webView.setVisibility(View.VISIBLE);
                webView.reload();
            }
        });
    }
}
